package com.example.test;

import android.app.Activity;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BaseActivity extends AppCompatActivity {

    /** 每个Activity的线程池。 */
    private static ExecutorService executor;
    /** 打开的Activity的集合。 */
    private static List<Activity> activityList;

    /**
     * 在子线程中执行任务
     *
     * @param runnable
     */
    public void runOnSubThread(Runnable runnable) {
        executor.execute(runnable);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (activityList == null) {
            activityList = new ArrayList<>();
        }
        if (executor == null) {
            executor = new ThreadPoolExecutor(
                    1,
                    1,
                    0L,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(Runnable runnable) {
                            Thread thread = new Thread(runnable);
                            thread.setName("activity-sub-thread-" + thread.getId());
                            return thread;
                        }
                    });

        }
        activityList.add(this);

        //initImmersionBar();
    }
    @Override
    protected void onDestroy() {
        activityList.remove(this);
        if (activityList.isEmpty()) {
            activityList = null;
            executor.shutdown();
            executor = null;
        }

        super.onDestroy();
    }

    protected void initImmersionBar() {
        ImmersionBar.with(this)
                //.transparentBar() //透明状态栏和导航栏，不写默认状态栏为透明色，导航栏为黑色（设置此方法，fullScreen()方法自动为true）
                .fullScreen(true) //有导航栏的情况下，activity全屏显示，也就是activity最下面被导航栏覆盖，不写默认非全屏
                .hideBar(BarHide.FLAG_HIDE_BAR)  //隐藏状态栏或导航栏或两者，不写默认不隐藏
                .autoDarkModeEnable(true)
                .init();  //必须调用方可应用以上所配置的参数
    }
}
